module app.repository.CategoryRepository;

import hunt.entity.repository;
import hunt.framework.Simplify;
import app.model.Category;
import app.model.Article;

// import std.format;

// import std.stdio;

class CategoryRepository : EntityRepository!(Category, ulong)
{
    this()
    {
        super(defaultEntityManager());
    }

    /**
    * 通过分类名称查询
    * @Parameters    name    string
    * @return    Category
    */
    Category finByName(string name)
    {
        return _manager.createQuery!(Category)("SELECT * FROM category WHERE name = :name LIMIT 1")
            .setParameter("name", name).getSingleResult();
    }

    /**
    * 通过分类id查询文章列表
    * @Parameters    cid    long
    * @return    Article[]
    */
    Article[] findArticsleByCateId(long cid)
    {
        // string sql = format("SELECT * FROM article WHERE article.cid = %s", cid);
        // writeln(sql);
        return _manager.createQuery!(Article)("SELECT * FROM article WHERE article.cid = :cid")
            .setParameter("cid", cid).getResultList();
    }
}
